package com.n9.qqonline.sales.service;

import com.n9.db.util.GenericService;
import com.n9.qqonline.sales.entity.User;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author ice
 * @since 2019/5/28 15:12
 */
public class UserService {
    /**
     * 获取用户
     * @return
     */
    public User getUser() {
        List<User> users = new GenericService().queryAll("FROM User ORDER BY lastLoginTime DESC");
        return users.isEmpty() ? null : users.get(0);
    }

    /**
     * 保存或更新用户
     * @param user
     */
    public void saveOrUpdate(User user) {
        GenericService genericService = new GenericService();
        User u = genericService.getByHql(String.format("FROM User where username = '%s'", user.getUsername()));
        user.setLastLoginTime(new Date());
        if (Objects.isNull(u)) {
            genericService.save(user);
        } else {
            genericService.update(user);
        }
    }
}
